<template>
  <div class="container">
    <el-card class="jtqCard">
      <el-row>
        <el-col :span="3">
          <el-form :model="form">
            <el-form-item style="margin-right: 10px; width: 200px" size="small">
              <el-input
                v-model="form.title"
                placeholder="根据用户名搜索"
              ></el-input>
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="12">
          <el-button size="small" @click="clearBtn">清除</el-button>
          <el-button type="primary" size="small" @click="searchKey"
            >搜索</el-button
          >
        </el-col>
        <el-button
          class="jtqBtn"
          icon="el-icon-edit"
          type="success"
          size="small"
          @click="add"
          >新增权限组</el-button
        >
      </el-row>
      <Alert :total="num" v-if="num && alertShow"></Alert>
      <el-table
        ref="refTable"
        :data="tableData"
        tooltip-effect="dark"
        style="width: 100%"
      >
        <el-table-column type="selection" width="55"> </el-table-column>
        <el-table-column prop="title" label="用户名" width="400" align="center">
        </el-table-column>
        <el-table-column prop="create_date" label="日期" align="center">
        </el-table-column>
        <el-table-column label="操作" width="150" align="center">
          <template v-slot="{ row }">
            <el-button
              circle
              plain
              type="primary"
              icon="el-icon-edit"
              @click="editDo(row)"
            ></el-button>
            <el-button
              circle
              plain
              type="danger"
              icon="el-icon-delete"
              @click="delData(row)"
            >
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <Pagination :page.sync="page" :pagesize.sync="pagesize" :total="num" />
      <!-- 弹层 -->
      <el-dialog :visible="showDialog" :title="btnTitle" @close="cancel">
        <el-form
          ref="refForm"
          :model="dialogForm"
          label-width="160px"
          style="width: 510px"
          :rules="rules"
        >
          <el-form-item label="用户名" prop="title">
            <el-input v-model="dialogForm.title"></el-input>
          </el-form-item>
          <el-form-item label="权限分配">
            <el-tree
              ref="treeRef"
              :data="treeData"
              show-checkbox
              node-key="id"
              :props="defaultProps"
              :default-expanded-keys="[0]"
              :default-checked-keys="dialogForm.checkedArr"
            >
            </el-tree>
          </el-form-item>
          <el-row type="flex" justify="center">
            <el-col :span="9">
              <el-button style="margin-left: 25px" size="small" @click="cancel"
                >取消</el-button
              >
              <el-button type="primary" size="small" @click="isOk"
                >确定</el-button
              >
            </el-col>
          </el-row>
        </el-form>
      </el-dialog>
    </el-card>
  </div>
</template>
<script>
import Alert from './../../components/Jtq/alert'
import { list, add, update, remove, detail } from './../../api/base/permissions'
import { list as listQX } from './../../api/base/menus'
import Pagination from './../../components/Jtq/pagination'
export default {
  name: 'Permissions',
  data () {
    return {
      // expanded: [0],
      rowId: null,
      treeData: null,
      tableData: null,
      showDialog: false,
      btnTitle: '',
      dialogForm: {
        title: '',
        checkedArr: []
      },
      rules: {
        title: [{ required: true, message: '用户名不能为空', trigger: 'blur' }]
      },
      form: {
        title: ''
      },
      num: null,
      alertShow: true,
      multipleSelection: [],
      page: 1,
      pagesize: 10,
      defaultProps: {
        children: 'points',
        label: 'title'
      }
    }
  },
  components: {
    Alert,
    Pagination
  },
  created () {
    this.listData()
    this.listQXData()
  },
  watch: {
    page () {
      this.listData()
    },
    pagesize () {
      this.listData()
    }
  },
  methods: {
    // 点击搜索
    async searchKey () {
      this.alertShow = false
      const res = await list({
        page: this.page,
        pagesize: this.pagesize,
        title: this.form.title
      })
      this.tableData = res.data.list
      this.num = res.data.counts
      this.alertShow = true
    },
    // 点击清空
    clearBtn () {
      this.form.title = ''
      this.listData()
    },
    // 点击确定
    async isOk () {
      await this.$refs.refForm.validate()
      try {
        if (this.rowId !== null) {
          // 编辑
          await update({
            id: this.rowId,
            title: this.dialogForm.title,
            permissions: this.$refs.treeRef.getCheckedKeys()
          })
        } else {
          // 添加
          await add({
            title: this.dialogForm.title,
            permissions: this.$refs.treeRef.getCheckedKeys()
          })
        }
        this.cancel()
      } catch (e) {
        console.log(e)
      }
    },
    // 点击取消
    cancel () {
      this.dialogForm = {
        title: '',
        checkedArr: []
      }
      this.$refs.refForm.resetFields()
      this.rowId = null
      this.showDialog = false
      this.$refs.treeRef.setCheckedKeys([])
      this.listData()
    },
    // 新增权限
    add () {
      this.showDialog = true
      this.btnTitle = '创建权限组'
    },
    // 编辑权限
    async editDo (row) {
      this.showDialog = true
      // this.$refs.treeRef.setCurrentNode([0])
      this.btnTitle = '编辑权限组'
      this.rowId = row.id
      const {
        data: { permissions }
      } = await detail({ id: this.rowId })
      console.log(permissions)
      this.dialogForm.checkedArr = permissions
      this.dialogForm.title = row.title
    },
    // 删除权限
    delData (row) {
      // 弹框
      this.$confirm('此操作将永久删除用户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          await remove({ id: row.id })
          this.$message({
            type: 'success',
            message: '成功删除了用户!'
          })
          this.listData()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消操作'
          })
        })
    },
    // 获取数据列表
    async listData () {
      this.alertShow = false
      const res = await list({
        page: this.page,
        pagesize: this.pagesize,
        title: this.form.title
      })
      this.num = res.data.counts
      this.tableData = res.data.list
      this.alertShow = true
    },
    // 获取权限列表
    async listQXData () {
      const { data } = await listQX()
      data.forEach((item) => {
        for (const key in item) {
          if (key === 'childs') {
            item.points = item[key]
          }
        }
      })
      this.treeData = [{ id: 0, title: '系统菜单和页面权限点', points: data }]
    }
  }
}
</script>

<style scoped lang="less"></style>
